package loengud.w05;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Code from week 5, 03.10.2013 lecture.
 * Some simpler operations with dates.
 * @author Ago
 *
 */
public class Dates {

	/**
	 * Main method.
	 * @param args Command line arguments
	 * @throws ParseException
	 */
	public static void main(String[] args) throws ParseException {
		String dateString = "11.08.2013 15:55";
		// 2013-09-12T12:55:12.123Z
		// see javadoc for format information
		SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm");
		Date d = sdf.parse(dateString);
		System.out.println(d);
		
		Calendar c = Calendar.getInstance();
		System.out.println(c.getTime());
		c.add(Calendar.DAY_OF_YEAR, -4);
		System.out.println(c.getTime());
		
		Calendar c1 = Calendar.getInstance();
		c1.setTime(d);
		c1.add(Calendar.HOUR, 12);
		System.out.println(c1.getTime());

		//Calendar c2 = Calendar.getInstance();
		//c2.setTime(d);
		Calendar c2 = (Calendar) c1.clone();
		c2.add(Calendar.HOUR, -12);
		System.out.println(c2.getTime());
		
		
		if (c1.after(c2)) {
			// compare Calendar object with Calendar object
		}
		if (c1.after(d)) {
			// you can also compare Calendar object with Date
		}
		
		// formatting
		// let's try to format our original date 
		// into a different format
		
		// We change the locale in order to get month name in english
		// otherwise it uses default locale, which in my case is 
		// estonian
		
		// the same applies for parsing - if you have month or weekday
		// names in English, you have to use locale like US or UK to parse
		// those correctly. Locale change has to be done 
		// BEFORE initializing SimpleDateFormat object
		Locale.setDefault(Locale.US);

		SimpleDateFormat sdf2 = new SimpleDateFormat("dd MMMM yyyy HH.mm.ss");
		System.out.println(sdf2.format(d));
	}

}
